# -*- coding: UTF-8 -*-
__author__ = 'beibei'
import socket,threading
import proxymanage
def initsockets(proxysIp):
    socket.setdefaulttimeout(1)
    sockets=[]
    for ip in proxysIp:
        try:
            s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            #s.connect((ip[0],int(ip[1])))
            print ip
            s.connect(ip)
            sockets.append(s)
        except:
            print 'error'
            s.close()
    return sockets


def send(socket, host='',page=''):
    print host,page
    sendok='''GET {1} HTTP/1.1
    Host: {0}
    Proxy-Connection: keep-alive
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
    Accept-Language: zh-CN,zh;q=0.8

    '''.format(host,page)

    print sendok;
    try:
        for i in range(0,200):
            socket.send(sendok)
    except:
        print 'error'
def proxysend(proxys, host='',page=''):
    print host,page
    sendok='''GET {1} HTTP/1.1
    Host: {0}
    Proxy-Connection: keep-alive
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
    Accept-Language: zh-CN,zh;q=0.8

    '''.format(host,page)

    for p in proxys:
        try:
            s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            s.connect(p)
            s.send(sendok)
            print s.recv(1024)
        except:
            print 'error',p
        finally:
            s.close()

def godo():
    proxys=proxymanage.initproxy()
    proxys=proxymanage.replacerealproxy(proxys)
    #sockets=initsockets(proxys)
    #print sockets
    threads=[]
    for i in range(0,100):
        t=threading.Thread(target=proxysend,args=(proxys,'www.ss.cn','http://www.ss.cn/',))
        threads.append(t)
        pass

    for t in threads:
        t.start()

    for t in threads:
        t.join()

    print 'ok';



if __name__ == '__main__':
    godo()



#
# sendok='''GET http://www.baidu.com/ HTTP/1.1
# Host: www.baidu.com
# Proxy-Connection: keep-alive
# Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
# Upgrade-Insecure-Requests: 1
# User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
# Accept-Language: zh-CN,zh;q=0.8
#
# '''
#
# mysendok='''
# GET http://www.xx.com/Handler/GetVerifyCode HTTP/1.1
# Host: www.xx.com
# Proxy-Connection: keep-alive
# Accept: image/webp,image/*,*/*;q=0.8
# User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
# Referer: http://d.aibianli.cn/Account/Login?ReturnUrl=%2f
# Accept-Encoding: gzip, deflate, sdch
# Accept-Language: zh-CN,zh;q=0.8
#
# '''
#
#
# sendproxconnect='''CONNECT www.xx.com:80 HTTP/1.1
# Host: www.xx.com
# Proxy-Connection: keep-alive
# User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
#
# '''
#
# s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# s.connect(('111.1.70.211',80))
# s.send(sendok)
# response =''
# buf=s.recv(1024)
# response+=buf
# while len(buf):
#     print buf
#     #print bytes.decode(buf,encoding='utf-8')
#     buf = s.recv(1024)
#     response+=buf
# print response
# s.send(sendok)
